from rest_framework import generics

from apps.app_friend.models import Friend
from apps.app_friend.serializers import FriendsSerializer
from apps.app_post.serializers import PostsSerializer, CategorySerializer, TagsSerializer
from apps.app_post.models import Post, Category, Tag
from apps.app_post.filter import PostsFilterBackend, RecommendPostsFilterBackend
from apps.app_talk.filter import TalksFilterBackend
from apps.app_talk.models import Talk, Mood
from apps.app_talk.serializers import TalksSerializer, MoodsSerializer
from utils.customPagination import CustomPagePagination
from utils.resultful import ResultCode


class GetPostsView(generics.ListAPIView):
    """
        获取数据的View
        self.filter_queryset(self.get_queryset()) 会执行 filter_backends 中的过滤器
    """
    # 数据源
    queryset = Post.objects.order_by('-pub_time')
    # 数据序列化格式
    serializer_class = PostsSerializer
    # 自定义分页
    pagination_class = CustomPagePagination
    # 数据过滤器
    filter_backends = [PostsFilterBackend]

    # 父类方法, 返回数据
    # def list(self, request, *args, **kwargs):
    #     queryset = self.filter_queryset(self.get_queryset())
    #     page_data = self.paginate_queryset(queryset)
    #     # page_data ==> [<Post: Post object (PY4t6Ze5ZfnznJdhDzycKM)>]
    #     # 指定 pagination_class时, 页数错误直接返回 {"detail": "无效页面。"}
    #     if page_data is not None:  # None 是未指定pagination_class
    #         serializer = self.get_serializer(page_data, many=True)
    #         # return self.get_paginated_response(serializer.data)
    #         return_dict = {
    #             'total': _total,
    #             'limit': 10,
    #             'list': _list
    #         }
    #         return Response(return_dict)
    #     # 未指定pagination_class 自定义返回
    #     # serializer = self.get_serializer(queryset, many=True)
    #     # return Response(serializer.data)


class GetRecommendPostView(generics.ListAPIView):
    queryset = Post.objects.order_by('-pub_time')
    serializer_class = PostsSerializer
    filter_backends = [RecommendPostsFilterBackend]

    # 上面serializer_class不能直接实例化，在这个函数上操作
    # 指定字段或者排除字段
    def get_serializer(self, *args, **kwargs):
        return super(GetRecommendPostView, self).get_serializer(exclude=('is_recom',), *args, **kwargs)

    def list(self, request, *args, **kwargs):
        serializer = super(GetRecommendPostView, self).list(request, *args, **kwargs)
        return ResultCode.ok(msg="获取推荐文章成功~~", data=serializer.data)


class GetCategoryView(generics.ListAPIView):
    queryset = Category.objects.order_by('-id')
    serializer_class = CategorySerializer
    pagination_class = CustomPagePagination


class GetTagsView(generics.ListAPIView):
    queryset = Tag.objects.order_by('-id')
    serializer_class = TagsSerializer
    pagination_class = CustomPagePagination


class GetTalksView(generics.ListAPIView):
    queryset = Talk.objects.order_by('-pub_time')
    serializer_class = TalksSerializer
    pagination_class = CustomPagePagination
    filter_backends = [TalksFilterBackend]


class GetMoodsView(generics.ListAPIView):
    queryset = Mood.objects.order_by('-id')
    serializer_class = MoodsSerializer
    pagination_class = CustomPagePagination


class GetFriendsView(generics.ListAPIView):
    queryset = Friend.objects.order_by('-pub_time')
    serializer_class = FriendsSerializer
    pagination_class = CustomPagePagination
